package com.it.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.it.mapper.InStoreMapper;
import com.it.mapper.OutStoreMapper;
import com.it.pojo.*;
import com.it.pojo.dto.OutStoreDto;
import com.it.pojo.dto.ProductDto;
import com.it.service.DiscretionService;
import com.it.service.OutStoreService;
import com.it.service.OutdisposalService;
import com.it.util.CodeUtils;
import com.it.util.TimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class OutStoreServiceImpl implements OutStoreService {

    static Double ALL_NUM = 0.0;
    @Autowired
    private OutStoreMapper outstoreMapper;
    @Autowired
    private DiscretionService discretionService;
    @Autowired
    private OutdisposalService outdisposalService;
    @Autowired
    private InStoreMapper inStoreMapper;

    @Override
    public int addOut(InStore instore, User user) {
        OutStore outstore=new OutStore();
        BeanUtils.copyProperties(instore,outstore);
        outstore.setOutId(getPrimaryKey());
        outstore.setOutNum(instore.getInNum());
        outstore.setOutCode(CodeUtils.getCodeByCountOT(Integer.parseInt(getPrimaryKey())));
        outstore.setOutUnit(instore.getInUnit());
        outstore.setOutTime(new Date());
        outstore.setOutDel(0);
        outstore.setUserId(user.getUserId());
        outstore.setUserName(user.getUserLoginname());
        int insert = outstoreMapper.insert(outstore);
        InStore store = inStoreMapper.selectByPrimaryKey(instore.getInId());
        double num = store.getInNum()- instore.getInNum();
        store.setInNum(num);
        ALL_NUM=ALL_NUM+instore.getInNum();
        if (store.getInNum()==0){
            //修改状态 该商品全部出库
            store.setInStatus(0);
            store.setInNum(ALL_NUM);
        }
        inStoreMapper.updateByPrimaryKeySelective(store);
        if(outstore.getOutStatus()==0){//自行利用
            discretionService.addDiscretion(outstore);
        }else{//委外处理
            outdisposalService.addOutdisposal(outstore,user);
        }
        return insert;
    }

    @Override
    public PageInfo<OutStore> findPage(OutStoreDto dto) {
        if (dto.getPageNo() == null) {
            dto.setPageNo(1);
        }
        PageHelper.startPage(dto.getPageNo(), dto.getPageSize());
        OutStoreExample example = new OutStoreExample();
        OutStoreExample.Criteria criteria = example.createCriteria();
        if (dto.getUserId() != null && dto.getUserId() != -1) {
            criteria.andUserIdEqualTo(String.valueOf(dto.getUserId()));
        }
        if (dto.getTime() != null && dto.getTime() != -1) {
            switch (dto.getTime()) {
                case 1:
                    criteria.andOutTimeBetween(TimeUtils.startDay(), TimeUtils.endDay());
                    break;
                case 2:
                    criteria.andOutTimeBetween(TimeUtils.startMonth(), TimeUtils.endMonth());
                    break;
                case 3:
                    criteria.andOutTimeBetween(TimeUtils.startSeason(), TimeUtils.endSeason());
                    break;
                case 4:
                    criteria.andOutTimeBetween(TimeUtils.startYear(), TimeUtils.endYear());
                    break;
            }
        }
        example.setOrderByClause("out_time asc");
        List<OutStore> list = outstoreMapper.selectByExample(example);
        PageInfo<OutStore> pageInfo = new PageInfo<>(list, 5);
        return pageInfo;
    }

    @Override
    public List<OutStore> findExcel(OutStoreDto dto) {
        OutStoreExample example = new OutStoreExample();
        OutStoreExample.Criteria criteria = example.createCriteria();
        if (dto.getUserId() != null && dto.getUserId() != -1) {
            criteria.andUserIdEqualTo(String.valueOf(dto.getUserId()));
        }
        if (dto.getTime() != null && dto.getTime() != -1) {
            switch (dto.getTime()) {
                case 1:
                    criteria.andOutTimeBetween(TimeUtils.startDay(), TimeUtils.endDay());
                    break;
                case 2:
                    criteria.andOutTimeBetween(TimeUtils.startMonth(), TimeUtils.endMonth());
                    break;
                case 3:
                    criteria.andOutTimeBetween(TimeUtils.startSeason(), TimeUtils.endSeason());
                    break;
                case 4:
                    criteria.andOutTimeBetween(TimeUtils.startYear(), TimeUtils.endYear());
                    break;
            }
        }
        example.setOrderByClause("out_time asc");
        return outstoreMapper.selectByExample(example);
    }

    public String getPrimaryKey(){
        OutStoreExample example=new OutStoreExample();
        List<OutStore> outstores = outstoreMapper.selectByExample(example);
        return outstores.size()+1+"";
    }
}
